package com.cqw.robot.base;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 基础路径类, 因为需要多次调用, 故封装成静态方法
 *
 * @author liupengFei
 * @date 2020/12/23
 */
public class BasePath {

    public static final String IMG_PATH = "img";
    public static final String LIB_PATH = "lib";
    public static final String PATH = "path";
    public static final String File_PATH = "filePath";

    public static String getImgPath() {
        return getFilePath(IMG_PATH);
    }

    public static String getLibPath() {
        return getFilePath(LIB_PATH);
    }

    public static String getFilePath(String filePath) {
        URL url = BasePath.class.getClassLoader().getResource("");
        Map<String, String> map = checkPath(filePath);
        String path = map.get(PATH);
        filePath = map.get(File_PATH);
        File file = new File(url.getPath() + path);
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getAbsolutePath() + filePath;
    }

    public static Map<String, String> checkPath(String filePath) {
        Map<String, String> map = new HashMap<>();
        map.put(PATH, "");
        if (StringUtils.isBlank(filePath)) {
            map.put(filePath, "");
            return map;
        }
        filePath = filePath.replaceAll("/", "\\");
        String index0 = filePath.substring(0, 1);
        if (!index0.equals("\\")) {
            filePath = "\\" + filePath;
        }
        map.put(filePath, filePath);
        List<String> pathList = Arrays.stream(StringUtils.split(filePath, "\\")).collect(Collectors.toList());
        if (pathList.isEmpty()) {
            return map;
        }
        String path = "";
        for (int i = 0; i < pathList.size() - 1; i++) {
            path = "\\" + pathList.get(i);
        }
        map.put(PATH, path);
        map.put(File_PATH, "\\" + pathList.get(pathList.size() - 1));
        return map;
    }

    public static void makePath(String type, String path) {
        if (StringUtils.isBlank(path)) {
            return;
        }
        if (path.indexOf(":\\") > 0) {
            // 如果包含 :\ 说明是电脑绝对路径，可以直接查找并新建路径
            List<String> list = Arrays.stream(StringUtils.split(path, "\\")).collect(Collectors.toList());
            if (list.size() > 1) {
                if (list.get(list.size() - 1).indexOf(".") > 0) {
                    /**
                     * 如果最后一个包含 . 则说明是个文件; 不包含，说明是个文件夹
                     * 如果是文件的话，删除list最后一个，并重新给path赋值
                     */
                    list.remove(list.size() - 1);
                    path = list.stream().collect(Collectors.joining("\\"));
                }
                if (!FileUtil.exist(path)) {
                    FileUtil.mkdir(path);
                }
            }
            return;
        } else if (path.indexOf(":\\") == 0) {
            throw new IllegalArgumentException("The Path Is Error");
        }
        type = type.replace("\\", "/");
        path = path.replace("\\", "/");
        while (StringUtils.isNotBlank(type) && "/".equals(type.substring(0, 1))) {
            int length = type.length();
            if (length < 1) {
                type = "";
            } else {
                type = type.substring(1, length);
            }
        }
        if (StringUtils.isBlank(type)) {
            throw new IllegalArgumentException("The Type Is Error");
        }
        URL url = BasePath.class.getClassLoader().getResource("");
        List<String> list = Arrays.stream(StringUtils.split(path, "/")).collect(Collectors.toList());
        if (list.size() > 1) {
            if (list.get(list.size() - 1).indexOf(".") > 0) {
                /**
                 * 如果最后一个包含 . 则说明是个文件; 不包含，说明是个文件夹
                 * 如果是文件的话，删除list最后一个，并重新给path赋值
                 */
                list.remove(list.size() - 1);
                path = "/" + list.stream().collect(Collectors.joining("/"));
            }
        } else if (path.indexOf(".") > 0) {
            path = "";
        } else if (path.indexOf(".") == 0) {
            throw new IllegalArgumentException("The Path Is Error");
        } else if (!"/".equals(path.substring(0, 1))) {
            path = "/" + path;
        }
        String dirPath = url.getPath() + type + path;
        if (!FileUtil.exist(dirPath)) {
            FileUtil.mkdir(dirPath);
        }
    }

}
